package com.cold.system.service.impl;

import com.cold.common.core.utils.MapstructUtils;
import com.cold.common.core.utils.StringUtils;
import com.cold.common.mybatis.core.page.TableDataInfo;
import com.cold.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.cold.system.domain.bo.TbWareHousesBo;
import com.cold.system.domain.vo.TbWareHousesVo;
import com.cold.system.domain.TbWareHouses;
import com.cold.system.mapper.TbWareHousesMapper;
import com.cold.system.service.ITbWareHousesService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 仓库信息Service业务层处理
 *
 * @author 鄧滔滔
 * @date 2024-10-19
 */
@RequiredArgsConstructor
@Service
public class TbWareHousesServiceImpl implements ITbWareHousesService {

    private final TbWareHousesMapper baseMapper;

    /**
     * 查询仓库信息
     *
     * @param id 主键
     * @return 仓库信息
     */
    @Override
    public TbWareHousesVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询仓库信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 仓库信息分页列表
     */
    @Override
    public TableDataInfo<TbWareHousesVo> queryPageList(TbWareHousesBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TbWareHouses> lqw = buildQueryWrapper(bo);
        Page<TbWareHousesVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的仓库信息列表
     *
     * @param bo 查询条件
     * @return 仓库信息列表
     */
    @Override
    public List<TbWareHousesVo> queryList(TbWareHousesBo bo) {
        LambdaQueryWrapper<TbWareHouses> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TbWareHouses> buildQueryWrapper(TbWareHousesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TbWareHouses> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getWarehouseName()), TbWareHouses::getWarehouseName, bo.getWarehouseName());
        lqw.like(StringUtils.isNotBlank(bo.getWarehouseAddress()), TbWareHouses::getWarehouseAddress, bo.getWarehouseAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getContactPerson()), TbWareHouses::getContactPerson, bo.getContactPerson());
        lqw.eq(StringUtils.isNotBlank(bo.getContactPhone()), TbWareHouses::getContactPhone, bo.getContactPhone());
        lqw.eq(bo.getStatus() != null, TbWareHouses::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增仓库信息
     *
     * @param bo 仓库信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TbWareHousesBo bo) {
        TbWareHouses add = MapstructUtils.convert(bo, TbWareHouses.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改仓库信息
     *
     * @param bo 仓库信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TbWareHousesBo bo) {
        TbWareHouses update = MapstructUtils.convert(bo, TbWareHouses.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TbWareHouses entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除仓库信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
